package nio.bytebuffer;

import java.nio.ByteBuffer;

/**
 * @author ZH
 */
public class TestByteBufferExam {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(32);
        buffer.put("hello\nI am zhang hao \n Ho".getBytes());
        split(buffer);
        buffer.put("w are u\n".getBytes());
        split(buffer);
    }
    public static void split(ByteBuffer source){
        source.flip(); //切换成读

        for (int i = 0; i < source.limit(); i++) {
            if (source.get(i) =='\n'){
                int length = i - source.position() + 1;
                ByteBuffer target = ByteBuffer.allocate(length);
                for (int j = 0; j < length; j++) {
                    target.put(source.get());
                }
                ByteBufferUtil.debugAll(target);
            }
        }
        source.compact(); //切换成写
    }
}
